package edu.uc.daoimpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import edu.uc.bean.Logger;
import edu.uc.dao.LoggerDao;
@Repository("loggerDao")
public class LoggerDaoImpl extends BaseDaoImpl<Logger> implements LoggerDao{

	@Override
	public Long insert(Logger bean) {
		Long  result  =  0L;
		bean.setInsertData(new Date());
		bean.setType("javaee");
		try  {
			// 保存
		getTmpl().save(bean);
		
		// 获取主键
		result  =  bean.getId();
		}  catch  (Exception  e)  {
	
			throw  new  RuntimeException(e);
		}  finally  {
	
		}
	
		return  result;
		}

	@Override
	public Long delete(Long id) {
		Long result = 0L;

		try {
			// 使用会话,获取特定主键的某个类型的对象
			Logger instance = getTmpl().get(Logger.class, id);
			// 如果存在,就删除
			if (instance != null) {
				getTmpl().delete(instance);
			}

			// 正常用提交,且数据存在, 则说明删除成功
			if (instance != null) {
				result = 1L;
			}
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Long update(Logger bean) {
		Long result = 0L;

		try {
			getTmpl().update(bean);

			// 正常用提交,且数据存在, 则说明删除成功
			result = 1L;
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Logger load(Long id) {
		Logger bean = null;

		try {
			// 使用会话,获取特定类型的主键对象
			bean = getTmpl().get(Logger.class, id);

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return bean;
	}

	@Override
	public List<Logger> list() {
		List<Logger> vList = new ArrayList<Logger>();
		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Logger ");
		sbHql.append(" order by id desc ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();
		try {

			vList = (List<Logger>) getTmpl().find(hql, params);

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}
		return vList;
	}

	@Override
	public Long count() {
		Long result = 0L;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from Logger ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Long countByName(String name) {
		Long result = 0L;

		name = "%" + name + "%";

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from Logger where message like ? ");

		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	//无
	@Override
	public Logger loadByName(String name) {
		Logger bean = null;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Logger where message=? ");
		sbHql.append(" order by id desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);
		Object[] params = arrParams.toArray();
		try {

			bean = getTmpl().execute(this.getCallbackBean(Logger.class, hql, params));

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}

		return bean;
	}

	@Override
	public List<Logger> pager(Long pageNum, Long pageSize) {
		//  TODO  Auto-generated  method  stub
	
		List<Logger>  vList  =  new  ArrayList<Logger>();
		Long  vStart  =  0L;
		//  Long  vEnd  =  0;
	
		vStart  =  (pageNum  -  1)  *  pageSize;//  下标从0开始,所以不要+1
		//  vEnd  =  pageNum  *  pageSize;
		Long  startIndex  =  vStart;
		StringBuilder  sbHql  =  new  StringBuilder();
		sbHql.append(" from Logger m");
		sbHql.append(" order by m.insertData desc");
		String  hql  =  sbHql.toString();
		List<Object>  arrParams  =  new  ArrayList<Object>();
		Object[]  params  =  arrParams.toArray();
		try  {
			vList  =  getTmpl().execute(this.getCallbackPager(Logger.class,startIndex,pageSize,hql,params));
		} 
		catch  (Exception  e)  {
			throw  new  RuntimeException(e);
		}  
		finally  {
		}
		return  vList;
	}

	//无
	@Override
	public List<Logger> pagerByName(String name, Long pageNum, Long pageSize) {
		List<Logger> vList = new ArrayList<Logger>();

		Long vStart = 0L;
		// Long vEnd = 0L;

		vStart = (pageNum - 1) * pageSize;// 不要+1
		// vEnd = pageNum * pageSize;

		name = "%" + name + "%";

		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Logger ");
		sbHql.append(" where message like ? ");
		sbHql.append(" order by id desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {
			vList = getTmpl().execute(this.getCallbackPager(Logger.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return vList;
	}

	@Override
	public Long loggerData(Logger bean) {
		Long  result  =  0L;
		
		try  {
			// 保存
		getTmpl().save(bean);
		
		// 获取主键
		result  =  bean.getId();
		}  catch  (Exception  e)  {
	
			throw  new  RuntimeException(e);
		}  finally  {
	
		}
	
		return  result;
		
	}

}
